/*
 * To change this template, choose Tools | Templates
 * This class is suppose to  manage the connection. It calls the server and the client and manages what goes to them
 */
package com.grapeshot.halfnes;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JLabel;

/**
 *
 * @author Sebastien Dion & Yome Messena Jean
 */
public class ConnectionHandler {
    
    public int intReadByte;
    public int intWriteByte;
    public boolean isConnected = false;
    public String clientIp;
    
    private CommunicationInterface communicator;
    
    public void connectClient(String ip){
        communicator = new Client();
        communicator.connect(ip);
        isConnected = true;
    }
    public void startServer(){
        communicator = new Server();
        communicator.connect();
        isConnected = true;
    }
    public void captureControl(){        
        //Maybe here I would/should force a polling of the controller. Not sure if necessary if I get my Observer pattern to work
    }
    public void writeInt(int message){
        communicator.sendData(message);
    }
    public int readInt(){
        intReadByte = communicator.getReceivedByte();
        return intReadByte;
    }

    public void manageDisgracefulDisconnection(){
        
    }
    
    public void kickUser(){
        if(isConnected){             
            communicator.close();
            communicator.connect();            
            isConnected = false;
        }
    }
    public void close() {
        if(communicator != null){            
            communicator.close();
            isConnected = false;
        }
    }

    public String getIp() {
        return communicator.getDistantIp();
    }

    String readLine() {
        return communicator.readLineServer();
    }
            
    
}
